// featurePackage/pages/oaManager/createActivity/createActivity.js
import {
  createActivityReq,   //创建活动
  updateActivityReq,   //更新活动
  delActivityReq,      //删除活动
} from "../../../../service/jb_activity_service"
import debounce from "../../../../tools/debounce"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      title: "", //标题

      introduce: "", //简介
      startTime: "",
      endTime: "",
    },
    showTimeText: "",
    activityId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (app.globalData.activityDetail.id) {
      this.setData({
        formData: app.globalData.activityDetail,
        activityId: app.globalData.activityDetail.id
      })
      this.dealShowTime();
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  dealShowTime() {
    let stTime = this.data.formData.startTime;
    let endTime = this.data.formData.endTime;
    this.setData({
      showTimeText: stTime.split(' ')[0] + ' - ' + endTime.split(' ')[0],
    })
  },

  cellInputChange(e) {
    let value = e.detail.value;
    let key = e.currentTarget.dataset.key;
    if (key == 'title') {
      this.setData({
        'formData.title': value,
      })
    }
  },
  //时间筛选回调
  timeConfirmAction(e) {
    console.log("JJLog:结果是:" + JSON.stringify(e));
    // {"type":"timeConfirmAction","timeStamp":34363,"target":{"id":"","dataset":{}},"currentTarget":{"id":"","dataset":{}},"mark":{},"detail":{"showTimeText":"2022-10-05 - 2022-10-12","startTimeStr":"2022-10-05 00:00:00","endTimeStr":"2022-10-12 23:59:59"},"mut":false}
    if (e.detail.startTimeStr == '') {
      this.setData({
        'formData.startTime': '',
        'formData.endTime': '',
      })
      return;
    }
    // let startAt = e.detail.startTimeStr.split(' ')[0];
    // let endAt = e.detail.endTimeStr.split(' ')[0];
    // this.setData({
    //   'formData.timeRange': startAt + "," + endAt,

    // })
    this.setData({
      'formData.startTime': e.detail.startTimeStr,
      'formData.endTime': e.detail.endTimeStr
    })


  },

  bindRemarkText(e) {
    this.setData({
      'formData.introduce': e.detail.value
    })
  },

  // 创建或者更新活动
  submitAction() {
    let dataInfo = this.data.formData;
    if (!dataInfo.title.trim()) {
      wx.showToast({
        title: '请填写活动标题',
        icon: "none"
      })
      return;
    }
    if (!dataInfo.startTime) {
      wx.showToast({
        title: '请选择活动时间',
        icon: "none"
      })
      return;
    }
    if (!dataInfo.introduce.trim()) {
      wx.showToast({
        title: '请填写活动简介',
        icon: "none"
      })
      return;
    }
    if (this.data.activityId) {
      //更新
      debounce(this.updateActivityAction)
    } else {
      debounce(this.createActivityAction)
    }

  },
  createActivityAction() {
    wx.showLoading();
    createActivityReq(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '创建活动成功',
          icon: "success"
        })

        setTimeout(() => {
          wx.navigateBack();
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "创建失败",
        icon: "none"
      })
      console.error("JJLog:createActivityReq", err);
    })
  },
  updateActivityAction() {
    wx.showLoading();
    updateActivityReq(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '更新成功',
          icon: "success"
        })

        setTimeout(() => {
          wx.navigateBack();
        }, 2000)
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "创建失败",
        icon: "none"
      })
      console.error("JJLog:updateActivityReq", err);
    })
  },

})